<template>

    <div class="layout" :class="{'layout-hide-text': spanLeft < 5}">
       <nprogress-container></nprogress-container>
       <template v-if="login">
        <Row type="flex">
            <asidebar :spanLeft="spanLeft"></asidebar>
            <i-col :span="spanRight" :iconSize="iconSize">
                <div class="layout-header">
                    <i-button type="text" @click="toggleClick">
                        <Icon type="navicon" size="32"></Icon>
                    </i-button>
                </div>
                
                <div class="layout-content">
                    <div class="layout-content-main"><router-view></router-view></div>
                </div>
                <footerbar></footerbar>
            </i-col>
        </Row>
        </template>

        <template v-else>
           <!-- <Form ref="formInline" :model="formInline" :rules="ruleInline" inline>
                <Form-item prop="name">
                    <Input type="text" v-model="formInline.name" placeholder="Username">
                        <Icon type="ios-person-outline" slot="prepend"></Icon>
                    </Input>
                </Form-item>
                <Form-item prop="password">
                    <Input type="password" v-model="formInline.password" placeholder="Password">
                    <Icon type="ios-locked-outline" slot="prepend"></Icon>
                    </Input>
                </Form-item>
                <Form-item>
                    <Button type="primary" @click="handleSubmit('formInline')">登录</Button>
                </Form-item>
            </Form> -->
        <div class="top">
            <div class="left"><img src="./images/logo.png" alt=""></div>
            <div class="right"><h2>即刻开启你的音乐生活</h2></div>
        </div>
            
            <div class="carousel">
                <template>
                    <Carousel
                        v-model="value3"
                        :autoplay="setting.autoplay"
                        :autoplay-speed="setting.autoplaySpeed"
                        :dots="setting.dots"
                        :trigger="setting.trigger"
                        :arrow="setting.arrow">
                        <Carousel-item >
                            <div class="demo-carousel"><img src="./images/banner_1.jpg" alt=""></div>
                        </Carousel-item>
                        <Carousel-item>
                            <div class="demo-carousel"><img src="./images/banner_2.jpg" alt=""></div>
                        </Carousel-item>
                        <Carousel-item>
                            <div class="demo-carousel"><img src="./images/banner_3.jpg" alt=""></div>
                        </Carousel-item>
                        <Carousel-item>
                            <div class="demo-carousel"><img src="./images/vip_1.jpg" alt=""></div>
                        </Carousel-item>
                        <Carousel-item>
                            <div class="demo-carousel"><img src="./images/vip_2.jpg" alt=""></div>
                        </Carousel-item>
                        <Carousel-item>
                            <div class="demo-carousel"><img src="./images/vip_3.jpg" alt=""></div>
                        </Carousel-item>
                    </Carousel>
                </template>
            </div>
            <div class="form1"> 
            <Row>
                <Col span="8" offset="10"> <img src="https://q1.qlogo.cn/g?b=qq&amp;k=Ws6YicsGibjHSQCicSiaGW1t0g&amp;s=140&amp;t=1487171673" onerror="this.src='//y.gtimg.cn/mediastyle/global/img/person_300.png?max_age=31536000';this.onerror=null;" alt="Waiting" class="profile__cover" id="profileHead">
                </Col>                        
            </Row> 
           
                <Row>
                        <Col span="8" offset="8">
                            <div class="header">
                                <p>心情，让你听见</p>
                                <p>音乐，不将就</p>
                                <p>爱豆，在一起</p>
                                <p>我，就要不一样</p>
                            </div>
                        </Col>                        
                    </Row>             
                
                <Form ref="formInline" :model="formInline" :rules="ruleInline">
                    
                    <Row>
                        <Col span="8" offset="9"><Form-item prop="name" ><Input type="text" v-model="formInline.name" placeholder="Username">
                    <Icon type="ios-person-outline" slot="prepend"></Icon>
                    </Input>  </Form-item>
                        </Col>                        
                    </Row>
                        
                  
                    <Row>
                        <Col span="8" offset="9"><Form-item prop="password">
                        <Input type="password" v-model="formInline.password" placeholder="Password">
                        <Icon type="ios-locked-outline" slot="prepend"></Icon>
                        </Input>
                        </Form-item>
                        </Col>                        
                    </Row>
                    
                        <Row>
                            <Col span="8" offset="11"><div class="submit"><Form-item>
                            <Button type="primary" @click="handleSubmit('formInline')">登录</Button>
                        </Form-item></div>
                            </Col>                        
                        </Row>
                    
                    
                    
                </Form>
            </div>
            
        </template>
    </div>

</template>

<script>
    import Footerbar from './components/layout/Footerbar.vue'
    import Asidebar from './components/layout/Asidebar.vue'
    import {
        mapGetters,
        mapActions
    } from 'vuex'
    var CryptoJS = require("crypto-js");
    import NprogressContainer from 'vue-nprogress/src/NprogressContainer.vue'
    
    export default {
        data() {
            return {
                value3: 0,
                setting: {
                    autoplay: true,
                    autoplaySpeed: 6000,
                    dots: 'inside',
                    trigger: 'click',
                    arrow: 'never'
                },
                spanLeft: 5,
                spanRight: 19,
                formInline: {
                    name: '1',
                    password: '111111'
                },
                ruleInline: {
                    name: [{
                        required: true,
                        message: '请填写用户名',
                        trigger: 'blur'
                    }],
                    password: [{
                            required: true,
                            message: '请填写密码',
                            trigger: 'blur'
                        },
                        {
                            type: 'string',
                            min: 6,
                            message: '密码长度不能小于6位',
                            trigger: 'blur'
                        }
                    ]
                }
            }
        },
        methods: {
            ...mapActions(['checkLogin']),
            handleSubmit(name) {
                // CryptoJS.MD5(_this.formValidate.password, { asString: true })
                let _this = this;
                _this.formInline.password = CryptoJS.MD5(_this.formInline.password, {
                    asString: true
                }).toString();

                console.log(_this.formInline)
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        _this.$http.post("http://localhost:3000/users/login", _this.formInline).then(res => {
                           
                            if(res.data==null){
                                this.$Message.error('用户名或密码错误!');
                            }
                            else if(res.data.name && res.data.name.trim().length > 0) {
                                this.checkLogin(true);
                            }
                        })  
                    } else {
                        this.$Message.error('表单验证失败!');
                    }
                })
            },
            toggleClick () {
                if (this.spanLeft === 5) {
                    this.spanLeft = 1;
                    this.spanRight = 23;
                } else {
                    this.spanLeft = 5;
                    this.spanRight = 19;
                }
            }
        },
        components: {
            Footerbar,
            Asidebar,
            NprogressContainer
        },
        computed: {
            ...mapGetters(['login']),
            iconSize () {
                return this.spanLeft === 5 ? 14 : 24;
            }
        },
        mounted() {
            this.$nextTick(() => {
                this.$nprogress.configure({
                    parent: '.nprogress-container'
                })
            });
        }
    }
</script>

<style lang="scss">
    .top{
        height:60px;
        .right{
            padding-top:20px;
            padding-right:30px;
        }
    }
    .left{
        float:left;
    }
    .right{
        float:right;
    }
    .demo-carousel{
        height:820px;
    }
    .form1{
        top:70px;
        position:absolute;
        width:100%;       
        margin:0 auto;
       .submit{
           margin-left:20px;
       }
       .header{           
           margin-top:10px;
           margin-bottom:20px;
           p{
               text-align:center;
               font-size:20px;
               color:white;
           }
       }
       .profile__cover {
            margin-left:28px;
            display: block;          
            border: 4px solid #fff;
            border-radius: 99px;
            background: #fff;
        }
        .ivu-input-group .ivu-input {
            width: 69%;
            float: left;
            margin-bottom: 0;
            position: relative;
            z-index: 2;
        }
    }
    
    .layout{
        border: 1px solid #d7dde4;
        background: #f5f7f9;
        position: relative;
        border-radius: 4px;
        overflow: hidden;
    }
    .layout-breadcrumb{
        padding: 10px 15px 0;
    }
    .layout-content{
        min-height: 200px;
        margin: 15px;
        overflow: hidden;
        background: #fff;
        border-radius: 4px;
    }
    .layout-content-main{
        padding: 10px;
    }
    .layout-copy{
        text-align: center;
        padding: 10px 0 20px;
        color: #9ea7b4;
    }
    .layout-menu-left{
        background: #464c5b;
    }
    .layout-header{
        height: 60px;
        background: #fff;
        box-shadow: 0 1px 1px rgba(0,0,0,.1);
    }
    .layout-logo-left{
        width: 90%;
        height: 30px;
        background: #5b6270;
        border-radius: 3px;
        margin: 15px auto;
    }
    .layout-ceiling-main a{
        color: #9ba7b5;
    }
    .layout-hide-text .layout-text{
        display: none;
    }
    .ivu-col{
        transition: width .2s ease-in-out;
    }
    
    .nprogress-container {
        position: fixed !important;
        width: 100%;
        height: 50px;
        z-index: 2048;
        pointer-events: none;
        #nprogress {
            $color: red;
            .bar {
                background: $color;
            }
            .peg {
                box-shadow: 0 0 10px $color, 0 0 5px $color;
            }
            .spinner-icon {
                border-top-color: $color;
                border-left-color: $color;
            }
        }
    }
</style>